package hou.just4fun.HupuTweets.work;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;

import hou.just4fun.HupuTweets.dao.TweetDao;
import hou.just4fun.HupuTweets.model.Tweet;
import hou.just4fun.HupuTweets.model.User;
import hou.just4fun.HupuTweets.util.JedisPoolFactory;
import hou.just4fun.HupuTweets.util.LRUCache;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author houweitao
 * @date 2016年5月15日上午12:50:16
 */

public class CrawlerHistory {

	private static final Logger log = LoggerFactory.getLogger(Crawler.class);
	private static final String url = "http://my.hupu.com/public/note-nba-new-";
	private static final JedisPool pool = new JedisPoolFactory().getInstance();
	private final String key = "HUPU$TWEET";
	private static LRUCache<String, Integer> lru = new LRUCache<>(500);
	private static TweetDao tweetDao;
	private static long crawlerNum = 1;

	{
		ApplicationContext context = new ClassPathXmlApplicationContext("DaoContext.xml");
		tweetDao = (TweetDao) context.getBean("tweetDao");
	}

	public static void main(String[] args) {
		CrawlerHistory c = new CrawlerHistory();
		c.work();
	}

	private void work() {
		boolean flag = true;
		int page = 2;
		while (flag) {
			String toCrawler = url + page++;
			if (page > 100)
				flag = false;
			try {
				Document doc = Jsoup.connect(toCrawler).followRedirects(true).timeout(10000)
						.userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)").get();

				Elements boxs = doc.select("div.bbs_box").select(".boxmain");

				for (Element box : boxs) {
					Element userHelp = box.select("a.u").first();
					String username = userHelp.text();
					String userurl = userHelp.attr("abs:href");
					User user = new User(username, userurl);
					// System.out.println(user);

					Element say = box.select("div.brief").first();
					String tweetUrl = say.select("a.blue").attr("abs:href");
					String tweetTime = say.select(".f666").text();
					if (!lru.containsKey(tweetUrl)) {
						log.info("new tweet:" + tweetUrl);
						lru.put(tweetUrl, 1);
						String tweetText = "";

						Element neirong = say.select("span.j").last();
						if (neirong != null)
							tweetText = neirong.text();
						else {
							say.select("p").remove();
							tweetText = say.text().trim();
						}
						Tweet tweet = new Tweet(user, tweetText, tweetUrl, tweetTime);
						System.out.println(tweet);
						saveTweet(tweet);
					} else {
						log.info("已包含:" + tweetUrl);
					}
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		log.info("已运行: " + crawlerNum++ + "次");
	}

	@SuppressWarnings("deprecation")
	void saveTweet(Tweet tweet) {
		tweetDao.saveTweet(tweet);

		Jedis jedis = pool.getResource();
		User userCur = tweet.getUser();
		User user = new User();
		if (!jedis.hexists(key, userCur.getUrl())) {
			log.info("不包含！");
			user = new User(tweet.getUser().getName(), tweet.getUser().getUrl());
		} else {
			log.info("有过！！");
			String userString = jedis.hget(key, userCur.getUrl());
			user = JSON.parseObject(userString, User.class);
			System.out.println("get: " + user);
		}
		user.getTweetMap().put(tweet.getUrl(), tweet);
		System.out.println(JSON.toJSONString(user));
		jedis.hset(key, userCur.getUrl(), JSON.toJSONString(user));
		log.info("end save..");

		try {
			pool.returnResource(jedis);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
		}
	}

}
